<template>
	<view class="send con_auto">
		<view class="con_auto">
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="80">
				<u-form-item label="用户" prop="user_ids" borderBottom ref="item1">
					<u-button
						:text="'选择用户(' + dataForm.user_ids.length + ')'"
						@tap.stop="selectUser"
						class="show_user"
						size="small"
						:customStyle="{ width: '200rpx' }"
					></u-button>
				</u-form-item>
				<u-form-item label="名称" prop="name" borderBottom ref="item1">
					<u--input v-model="dataForm.name" disabled disabledColor="#ffffff" border="none"></u--input>
				</u-form-item>
				<u-form-item label="发送消息" prop="is_send" borderBottom ref="item1">
					<u-switch v-model="dataForm.is_send" :active-value="1" :inactive-value="0"></u-switch>
				</u-form-item>
			</u--form>
			<view class="submit">
				<u-button text="赠送" type="primary" @tap.stop="send_data" :loading="is_loading" :disabled="is_loading"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			dataForm: {
				user_ids: [],
				name: '',
				id: 0,
				is_send: 0
			},
			is_loading:false
		};
	},
	onLoad({ id, name }) {
		this.dataForm.id = id;
		this.dataForm.name = name;
		this.$initConfig().then((_) => {
			
		});
	},
	methods: {
		send_data() {
			const _this = this
			_this.is_loading = true
			_this.$http('coupon/send_user',{
				..._this.dataForm
			}).then(res=>{
				if(res.code){
					uni.$u.toast('发送成功！')
					setTimeout(()=>{
						_this.is_loading = false
						uni.navigateBack()
					},1000)
				}
			}).catch(()=>{
				_this.is_loading = false
			})
		},
		selectUser() {
			const _this = this;
			wx.qy.checkSession({
				success: function () {
					console.log('未过期');
					//session_key 未过期，并且在本生命周期一直有效
					_this.selectExternalContact();
				},
				fail: function () {
					console.log('已失效');
					// session_key 已经失效，需要重新执行登录流程
					_this.$initConfig(true).then((_) => {
						_this.selectExternalContact();
					});
				}
			});
		},
		selectExternalContact() {
			const _this = this;
			wx.qy.selectExternalContact({
				filterType: 0,
				success(e) {
					console.log(e.userIds)
					_this.dataForm.user_ids = e.userIds;
				},
				fail(err) {
					console.log(err);
					_this.$u.toast(err.errMsg);
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.send {
	background: #fff;
}
.submit {
	margin-top: 24rpx;
	padding-bottom: 30rpx;
}
</style>
